package controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Result;
import pojo.User;
import service.UserService;

import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:5173")
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "重置用户密码", notes = "管理员重置用户密码为默认值")
    @PutMapping("/resetPassword/{userId}")
    public Result resetUserPassword(
            @ApiParam(value = "用户ID", required = true) @PathVariable Integer userId
    ) {
        try {
            boolean success = userService.resetUserPassword(userId);
            if (success) {
                return Result.success("密码已重置为默认值");
            } else {
                return Result.fail("重置失败，用户ID不存在");
            }
        } catch (IllegalArgumentException e) {
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            return Result.fail("重置失败：" + e.getMessage());
        }
    }
    @ApiOperation(value = "更新用户状态", notes = "启用或禁用用户账号")
    @PutMapping("/status/{userId}")
    public Result updateUserStatus(
            @ApiParam(value = "用户ID", required = true) @PathVariable Integer userId,
            @ApiParam(value = "状态值（启用/禁用）", required = true) @RequestParam String status
    ) {
        try {
            boolean success = userService.updateUserStatus(userId, status);
            if (success) {
                return Result.success("用户状态更新成功");
            } else {
                return Result.fail("更新失败，用户ID不存在");
            }
        } catch (IllegalArgumentException e) {
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            return Result.fail("更新失败：" + e.getMessage());
        }
    }
    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户详细信息")
    @GetMapping("/{id}")
    public User getUserById(@ApiParam(value = "用户ID", required = true) @PathVariable("id") Integer userId) {
        return userService.selectUserById(userId);
    }
    @ApiOperation(value = "根据用户名获取用户", notes = "根据用户名获取用户详细信息")
    @GetMapping("/username/{username}")
    public User getUserByUsername(@ApiParam(value = "用户名", required = true) @PathVariable String username) {
        return userService.selectUserByUsername(username);
    }

    @ApiOperation(value = "获取所有用户", notes = "获取系统中所有用户列表")
    @GetMapping("/list")
    public List<User> getAllUsers() {
        return userService.selectAllUsers();
    }
    @ApiOperation(value = "个人设置 - 更新用户部分信息", notes = "根据User对象更新用户部分信息")
    @PutMapping("/personal/settings")
    public int updateUserPersonalSettings(@ApiParam(value = "用户实体，只需包含要更新的字段", required = true) @RequestBody User user) {
        return userService.updateUserPersonalSettings(user);
    }
}
